package com.selenium;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
 * @author wq
 * @since 2022-06-20 16:16
 */

public class OpenBrowser {
    public static void main(String[] args) throws InterruptedException {
        RemoteWebDriver driver = test("chrome");
        driver.get("http://www.baidu.com");
        Thread.sleep(5000);
        driver.quit();
//        System.setProperty("webdriver.chrome.driver","src/test/resources/chromedriver.exe");
//        ChromeDriver driver = new ChromeDriver();
//        driver.get("http://www.baidu.com");
    }

    public static RemoteWebDriver test(String driverName){
        if("chrome".equalsIgnoreCase(driverName)){
            System.setProperty("webdriver.chrome.driver","src/test/resources/chromedriver.exe");
            ChromeDriver driver = new ChromeDriver();
            return driver;
        }else if ("firefox".equalsIgnoreCase(driverName)){

        }
        return  null;
    }

    /*
    打开浏览器
    打开百度
    等待2s
    浏览器后退
    等待2s
    关闭浏览器
     */
    @Test
    public void backTest() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","src/test/resources/chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        //打开浏览器
        driver.get("http://www.baidu.com");
        //等待2s
        Thread.sleep(2000);
        //浏览器后退
        driver.navigate().back();
        //等待2s
        Thread.sleep(2000);
        //关闭浏览器
        driver.quit();

    }

    /*
    打开浏览器
    打开百度
    等待2s
    浏览器前进
    等待2s
    关闭浏览器
     */
    @Test
    public void forwardTest() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","src/test/resources/chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        //打开浏览器
        driver.get("http://www.baidu.com");
        //等待2s
        Thread.sleep(2000);
        //浏览器后退
        driver.navigate().back();
        //等待2s
        Thread.sleep(2000);
        //浏览器后退
        driver.navigate().forward();
        //driver.get("https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1&rsv_idx=1&tn=54093922_20_hao_pg&wd=%E7%81%AB%E7%8B%90%E6%B5%8F%E8%A7%88%E5%92%8C%E9%A9%B1%E5%8A%A8%E5%AF%B9%E5%BA%94%E7%9A%84%E7%89%88%E6%9C%AC&fenlei=256&rsv_pq=eadecae80007b514&rsv_t=e81ahYcT8vU7luZ%2BcWpZbb2O8qJAkEbmXXn7GLLfpigJW3XTxx7t5%2BRENjF8qVJdDQEAPUUQQjoI&rqlang=cn&rsv_enter=1&rsv_dl=ih_0&rsv_sug3=1&rsv_sug1=1&rsv_sug7=001&rsv_sug2=1&rsv_btype=i&rsp=0&rsv_sug9=es_2_1&rsv_sug4=1523&rsv_sug=9");
        //等待2s
        Thread.sleep(2000);
        //关闭浏览器
        driver.quit();
    }

    /*
    打开浏览器
    打开谷歌首页
    等待2s
    刷新
    等待2s
    关闭浏览器
     */
    @Test
    public void refreshTest() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","src/test/resources/chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        //打开浏览器
        driver.get("https://www.google.com");
        //等待2s
        Thread.sleep(2000);
        //浏览器后退
        driver.navigate().refresh();
        //等待2s
        Thread.sleep(2000);
        //关闭浏览器
        driver.quit();
    }

    /*
   打开谷歌首页
   设置窗口大小 300*300
   等待2s
   窗口最大化
   等待2s
   关闭浏览器
    */
    @Test
    public void winTest() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","src/test/resources/chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        //打开浏览器
        driver.get("https://www.baidu.com");
        //设置窗口大小300*300，需要用到新的实例
        Dimension dimension = new Dimension(300,300);
        //最大化窗口
        driver.manage().window().setSize(dimension);
        //等待2s
        Thread.sleep(2000);
        //窗口最大化
        driver.manage().window().maximize();
        //等待2s
        Thread.sleep(2000);
        //关闭浏览器
        driver.quit();
    }

    /*
   打开谷歌首页
   获取url
   等待2s
   比较获取的值是否和预期一样
   等待2s
   关闭浏览器
    */
    @Test
    public void getURLTest() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","src/test/resources/chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        //打开浏览器
        driver.get("https://www.baidu.com");
        //获取url
        String url = driver.getCurrentUrl();
        System.out.println(url);
        //等待2s
        Thread.sleep(2000);
        //比较和预期是否一致
        Assert.assertEquals(url,"https://www.baidu.com/");
        //关闭浏览器
        driver.quit();
    }
}
